package gui.viewEntity;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import framework.classification.entities.ClAbstractEntity;
import framework.entity.ACLComponent;
import framework.entity.ACLEntity;
import framework.entity.ACLFunction;
import framework.entity.ACLInterface;

import gui.controller.Controller;
import gui.utils.DegradeVerticalPanel;
import gui.utils.DegradedPanel;
import gui.utils.JCoolButton;
import gui.utils.JCoolToggleButton;
import gui.utils.JRoundButton;
import gui.viewLattice.ViewPDF;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

public class ViewClEntity extends JPanel implements ActionListener{

	private ACLEntity aclEntity;
	private ClAbstractEntity cl;

	private Controller controller;

	private JCoolToggleButton connected;
	private JCoolToggleButton remplaced;
	private JCoolToggleButton beRemplaced;
	
	

	private JList list1;
	private JList list2;


	
	private ViewPDF canvaPDF;
	private String type;

	public ViewClEntity(ACLEntity acl,ClAbstractEntity cl,Controller controller) {
		super();
		this.type = acl.getType();
		this.cl = cl;
		this.aclEntity = acl;

		this.controller = controller;
		this.setLayout(new BorderLayout());
		this.setBackground(new Color(230,230,230));
		this.add(createComponent(),BorderLayout.CENTER);
		remplaced.setSelected(true);
		remplaced.doClick();
		if (aclEntity.getType() == ACLComponent.ACL_COMPONENT) connected.setVisible(false);

	}

	private JComponent createButtonGroup(){
		JPanel panel = new DegradeVerticalPanel(new Color(195,195,195),new Color(110,110,110));
		panel.setMaximumSize(new Dimension(10000,25));
		ButtonGroup group = new ButtonGroup();
		connected = new JCoolToggleButton("Connects with");connected.addActionListener(this);
		remplaced = new JCoolToggleButton("Replaces");remplaced.addActionListener(this);
		beRemplaced = new JCoolToggleButton("Can be replaced by");beRemplaced.addActionListener(this);
		group.add(connected);group.add(remplaced);group.add(beRemplaced);



		Box mainbox = Box.createHorizontalBox();
		mainbox.add(remplaced);
		mainbox.add(Box.createHorizontalStrut(5));
		mainbox.add(beRemplaced);
		mainbox.add(Box.createHorizontalStrut(5));
		mainbox.add(connected);
		panel.add(mainbox);

		return panel;
	}

	private JComponent createCenterComponent(){
		Box mainBox = Box.createVerticalBox();
		

		JPanel panelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel("Lattice");
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(400,25));
		panelLabel.setMaximumSize(new Dimension(400,25));

		label.setOpaque(true);
		label.setForeground(Color.WHITE);
		label.setBackground(Color.BLACK);



		try {
			canvaPDF = new ViewPDF(cl);
			canvaPDF.setPreferredSize(new Dimension(400,400));
			canvaPDF.setMaximumSize(new Dimension(400,400));
			canvaPDF.setBackground(Color.WHITE);
			canvaPDF.addMouseListener(controller);
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}




		mainBox.add(panelLabel);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(canvaPDF);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(Box.createVerticalGlue());
		return mainBox;
	}

	private JComponent createRightComponent(){
		Box mainBox = Box.createVerticalBox();

		JPanel panelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel("Abstract elements");
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(400,25));
		panelLabel.setMaximumSize(new Dimension(400,25));

		Vector<String> c = new Vector<String>();
		
		list2 = new JList(c);
		list2.addMouseListener(controller);
		JScrollPane slist2 = new JScrollPane(list2);
		slist2.setPreferredSize(new Dimension(400,400));
		slist2.setMaximumSize(new Dimension(400,400));
		mainBox.add(panelLabel);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(slist2);
		mainBox.add(Box.createVerticalGlue());
		return mainBox;

	}
	private JComponent createLeftComponent(){
		Box mainBox = Box.createVerticalBox();

		JPanel panelLabel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JLabel label = new JLabel("Defined elements");
		label.setForeground(Color.WHITE);
		panelLabel.add(label);
		panelLabel.setBackground(Color.BLACK);
		panelLabel.setPreferredSize(new Dimension(400,25));
		panelLabel.setMaximumSize(new Dimension(400,25));

		Vector<String> c = new Vector<String>();
		list1 = new JList(c);
		list1.addMouseListener(controller);
		JScrollPane slist1 = new JScrollPane(list1);
		slist1.setPreferredSize(new Dimension(400,400));
		slist1.setMaximumSize(new Dimension(400,400));

		mainBox.add(panelLabel);
		mainBox.add(Box.createVerticalStrut(10));
		mainBox.add(slist1);
		mainBox.add(Box.createVerticalGlue());
		return mainBox;
	}

	private JComponent createComponent(){


		Box hori = Box.createHorizontalBox();
		hori.add(Box.createHorizontalStrut(5));
		hori.add(createLeftComponent());
		hori.add(Box.createHorizontalStrut(5));
		hori.add(createCenterComponent());
		hori.add(Box.createHorizontalStrut(5));
		hori.add(createRightComponent());
		hori.add(Box.createHorizontalStrut(5));

		Box mainbox = Box.createVerticalBox();
		mainbox.add(createButtonGroup());
		mainbox.add(Box.createVerticalStrut(25));
		mainbox.add(hori);

		return mainbox;
	}

	public void actionPerformed(ActionEvent a) {
		Object o = a.getSource();
		ArrayList<String> data = null;
		if (o  == remplaced){
			data = cl.getSubstituteElement(aclEntity.toString());
		   try {
				canvaPDF.updatePDF();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (o == connected){
			data = cl.getConnectedElement(aclEntity.toString());
			try {
				canvaPDF.updatePDF();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
		else if (o == beRemplaced){
			data = cl.getCanbeSubstitutedElement(aclEntity.toString());
			try {
				canvaPDF.updatePDF();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
		ArrayList<String> data1 = new ArrayList<String>();
		ArrayList<String> data2 = new ArrayList<String>();

		for (String s : data){
			ACLEntity sub = null;
			if (aclEntity.getType() == ACLComponent.ACL_COMPONENT){
				sub = new ACLComponent(s,ACLFunction.MODE2);

				if (!cl.getAbstractClassfication().getStructure().getHierarchyUp().containsKey(sub.getName()))
					data2.add(sub.getName());
				else
					data1.add(sub.getName());

			}
			else if (aclEntity.getType() == ACLInterface.ACL_INTERFACE){
				sub = new ACLInterface(s,ACLFunction.MODE2);
				if (!cl.getAbstractClassfication().getStructure().getHierarchyUp().containsKey(sub.getName()))
					data2.add(sub.getName());
				else
					data1.add(sub.getName());
			}
			else{

				sub = new ACLFunction(s,ACLFunction.MODE2);
				if (cl.isAbstract(sub.toString()))
					data2.add(sub.toString());
				else
					data1.add(sub.toString());
				}
			}



		

		list1.setListData(data1.toArray());
		list2.setListData(data2.toArray());
		this.updateUI();
	}

	public String getMetriqueLattice(){
		String s = "<html>";
		
		s = s + "Role : "+cl.getType()+"<br/>";
		s = s + "Real elements : "+cl.getEntity().size()+"<br/>";
		s = s + "Abstract elements : "+cl.getLattice().getConceptLattice().getNewConcepts().size()+"<br/>";
		s = s + "</html>";
		return s;
	}
	
	
	public void update(){




	}
}
